package com.example.tools;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.example.pojos.Product;
import com.example.pojos.Product_Type;

public class ParserXMLHandler extends DefaultHandler {
	private final String mylittlemarket = "mylittlemarket";
	private final String category = "category";
	private final String product = "product";
	private final String name = "name";
	private final String shortDesc = "short";
	private final String longDesc = "long";
	private final String url = "url";
	private final String price = "price";
	
	private HashMap<Product_Type, ArrayList<Product>> entries;
	private Product myProduct;
	private StringBuffer buffer;
	private Product_Type categorie;
	
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		entries = new HashMap<Product_Type, ArrayList<Product>>();
		entries.put(Product_Type.MUSIQUE, new ArrayList<Product>());
		entries.put(Product_Type.VIDEO, new ArrayList<Product>());
		entries.put(Product_Type.IMAGE, new ArrayList<Product>());
		entries.put(Product_Type.DEVICE, new ArrayList<Product>());
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		buffer = new StringBuffer();
		
		if(localName.equalsIgnoreCase(mylittlemarket)){}
		else if(localName.equalsIgnoreCase(category)){
			String categorieName = attributes.getValue("name");
			if(categorieName.equalsIgnoreCase("photo"))
				this.categorie = Product_Type.IMAGE;
			else if(categorieName.equalsIgnoreCase("video"))
				this.categorie = Product_Type.VIDEO;
			else if(categorieName.equalsIgnoreCase("music"))
				this.categorie = Product_Type.MUSIQUE;
			else if(categorieName.equalsIgnoreCase("device"))
				this.categorie = Product_Type.DEVICE;
		}
		else if(localName.equalsIgnoreCase(product)){
			myProduct = new Product();
			myProduct.setCategorie(categorie);
		}
		/*else if(localName.equalsIgnoreCase(name)){}
		else if(localName.equalsIgnoreCase(shortDesc)){}
		else if(localName.equalsIgnoreCase(longDesc)){}
		else if(localName.equalsIgnoreCase(url)){}
		else if(localName.equalsIgnoreCase(price)){}*/
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equalsIgnoreCase(mylittlemarket)){}
		else if(localName.equalsIgnoreCase(category)){
			categorie = null;
		}
		else if(localName.equalsIgnoreCase(product)){
			entries.get(categorie).add(myProduct);
		}
		else if(localName.equalsIgnoreCase(name)){
			myProduct.setName(buffer.toString());
		}
		else if(localName.equalsIgnoreCase(shortDesc)){
			myProduct.setShortDesc(buffer.toString());
		}
		else if(localName.equalsIgnoreCase(longDesc)){
			myProduct.setLongDesc(buffer.toString());
		}
		else if(localName.equalsIgnoreCase(url)){
			myProduct.setUrl(buffer.toString());
		}
		else if(localName.equalsIgnoreCase(price)){
			myProduct.setPrice(Float.parseFloat(buffer.toString()));
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String tmp = new String(ch,start,length);
		if( buffer!= null )
			buffer.append(tmp);
	}
	
	public HashMap<Product_Type, ArrayList<Product>> getEntries(){
		return entries;
	}
}
